/*   The MIT License
 *   
 *   Carina Engine
 *   Copyright (c) 2009 2010 2011 2012 Zdravko Velinov
 *   
 *   Permission is hereby granted, free of charge, to any person obtaining a copy
 *   of this software and associated documentation files (the "Software"), to deal
 *   in the Software without restriction, including without limitation the rights
 *   to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 *   copies of the Software, and to permit persons to whom the Software is
 *   furnished to do so, subject to the following conditions:
 *
 *   The above copyright notice and this permission notice shall be included in
 *   all copies or substantial portions of the Software.
 *
 *   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 *   IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 *   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 *   AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 *   LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 *   OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 *   THE SOFTWARE.
 */

#include "carina/box.hh"

namespace Carina
{
Box::Box(ResourceManager& res_mgr, const Vector3& radius)
    :   m_Radius(radius)
{
    RendererPtr renderer = res_mgr.getRenderer();

    Vector3 box_data[] =
    {
        Vector3(-1.0f, -1.0f, -1.0f), Vector3( 1.0f, -1.0f, -1.0f),
        Vector3(-1.0f, -1.0f, -1.0f), Vector3(-1.0f,  1.0f, -1.0f),
        Vector3(-1.0f, -1.0f, -1.0f), Vector3(-1.0f, -1.0f,  1.0f),
        Vector3( 1.0f, -1.0f,  1.0f), Vector3(-1.0f, -1.0f,  1.0f),
        Vector3( 1.0f, -1.0f,  1.0f), Vector3( 1.0f,  1.0f,  1.0f),
        Vector3( 1.0f, -1.0f,  1.0f), Vector3( 1.0f, -1.0f, -1.0f),
        Vector3( 1.0f,  1.0f, -1.0f), Vector3(-1.0f,  1.0f, -1.0f),
        Vector3( 1.0f,  1.0f, -1.0f), Vector3( 1.0f, -1.0f, -1.0f),
        Vector3( 1.0f,  1.0f, -1.0f), Vector3( 1.0f,  1.0f,  1.0f),
        Vector3(-1.0f,  1.0f,  1.0f), Vector3( 1.0f,  1.0f,  1.0f),
        Vector3(-1.0f,  1.0f,  1.0f), Vector3(-1.0f, -1.0f,  1.0f),
        Vector3(-1.0f,  1.0f,  1.0f), Vector3(-1.0f,  1.0f, -1.0f)
    };

    InputElementArray arr(1);
    arr[0].set("POSITION", VT_RGB32F, 0);

    m_Prog = res_mgr.getShaderProgram("white");
    m_Layout = renderer->createInputLayout(m_Prog, arr);
    m_VBAxis = renderer->createVideoBuffer(CE_VERTEX_BUFFER, sizeof(box_data), box_data, CE_STATIC_DRAW);
}

void Box::setRadius(const Vector3& radius)
{
    m_Radius = radius;
}

Vector3 Box::getRadius(const Vector3& radius)
{
    return radius;
}

void Box::draw(Scene& scene, const Matrix4& transform)
{
    RendererPtr renderer = scene.getRenderer();
    Matrix4 tmat = scene.getCamera()->getTransformMatrix() * transform;
    tmat.scale(m_Radius);
    renderer->useShaderProgram(m_Prog);
    m_Prog->getVariable("TransformMatrix")->set(tmat);
    renderer->setInputLayout(m_Layout);
    renderer->setVideoBuffer(m_VBAxis, sizeof(Vector3));
    renderer->draw(CE_LINE_LIST, 0, 24);
}
}